package com.ruoyi.gpt.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.core.config.utils.WxGptUtil;
import com.ruoyi.gpt.domain.WxMsg;
import com.ruoyi.gpt.domain.vo.GptUserVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gpt.domain.vo.WxMsgVo;
import com.ruoyi.gpt.domain.bo.WxMsgBo;
import com.ruoyi.gpt.service.IWxMsgService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 微信消息
 *
 * @author ztx
 * @date 2023-03-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wx/gpt/chatMsg")
public class WxMsgController extends BaseController {

    private final IWxMsgService iWxMsgService;

    /**
     * 查询微信消息列表
     */
    // @SaCheckPermission("gpt:chatMsg:list")
    @GetMapping("/list")
    public TableDataInfo<WxMsgVo> list(WxMsgBo bo, PageQuery pageQuery) {
        GptUserVo userVo = WxGptUtil.getUserVoByToken();
        bo.setUserId(userVo.getId());
        return iWxMsgService.queryPageList(bo, pageQuery);
    }
    /**
     * 查询微信消息列表，管理端
     */
    @SaCheckPermission("gpt:chatMsg:list")
    @GetMapping("/listForAdmin")
    public TableDataInfo<WxMsgVo> listForAdmin(WxMsgBo bo, PageQuery pageQuery) {
        //以人员分组获取列表 查询微信消息的用户分组列表
        //iWxMsgService.queryPageListByGptUser(bo, pageQuery);
        //不分组查所有
        return iWxMsgService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询消息历史记录
     * @param bo
     * @param pageQuery
     * @return
     */
    @GetMapping("/detailList")
    public TableDataInfo<WxMsg> detailList(WxMsgBo bo, PageQuery pageQuery) {
        GptUserVo userVo = WxGptUtil.getUserVoByToken();
        bo.setUserId(userVo.getId());
        bo.setInOut("2");
        if (pageQuery.getPageNum() * pageQuery.getPageSize() > 20) {
            //最多查询20条
            TableDataInfo<WxMsg> obj = new TableDataInfo<>();
            obj.setCode(0);
            obj.setTotal(0);
            obj.setMsg("最多查询20条历史数据");
            obj.setRows(Arrays.asList());
            return obj;
        }
        return iWxMsgService.queryDetailPageList(bo, pageQuery);
    }

    /**
     * 导出微信消息列表
     */
    @SaCheckPermission("gpt:chatMsg:export")
    @Log(title = "微信消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxMsgBo bo, HttpServletResponse response) {
        List<WxMsgVo> list = iWxMsgService.queryList(bo);
        ExcelUtil.exportExcel(list, "微信消息", WxMsgVo.class, response);
    }

    /**
     * 获取微信消息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("gpt:chatMsg:query")
    @GetMapping("/{id}")
    public R<WxMsgVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iWxMsgService.queryById(id));
    }

    /**
     * 新增微信消息
     */
    @SaCheckPermission("gpt:chatMsg:add")
    @Log(title = "微信消息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxMsgBo bo) {
        return toAjax(iWxMsgService.insertByBo(bo));
    }

    /**
     * 修改微信消息
     */
    @SaCheckPermission("gpt:chatMsg:edit")
    @Log(title = "微信消息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxMsgBo bo) {
        return toAjax(iWxMsgService.updateByBo(bo));
    }

    /**
     * 删除微信消息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("gpt:chatMsg:remove")
    @Log(title = "微信消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iWxMsgService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
